package com.jk.smartelderlycare.controller;

import com.jk.smartelderlycare.dao.TbItemDao;
import com.jk.smartelderlycare.dto.TbItem;
import com.jk.smartelderlycare.entity.Item;
import com.jk.smartelderlycare.pojo.ResponseResult;
import com.jk.smartelderlycare.service.TbItemServiceFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hhk
 * @version 1.0
 * @ClassName TbItemController
 * @program: smart-elderly-care
 * @description: TODO
 * @date 2024/11/17 19:18
 */
@Api(tags = "商品索引操作模块")
@Slf4j
@RestController
@RequestMapping(value = "tbItem")
public class TbItemController {
    @Autowired
    private TbItemServiceFeign tbItemServiceFeign;

    @Autowired
    private TbItemDao tbItemDao;

    /** 
     * @description: 更新商品索引
     * @param: [tbItem]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/17 19:23
     */ 
    @ApiOperation(value = "更新商品索引",httpMethod = "POST",notes = "更新商品索引")
    @PostMapping(value = "updataTbItemIndex")
    public ResponseResult updataTbItemIndex(@RequestBody TbItem tbItem){

        //1.新建一个集合item 转换为es索引库
        List<Item> list = new ArrayList<>();
        //2.查询出所有审核通过商品
        try {
            List<TbItem> tbItemList = tbItemServiceFeign.findTbItemList(tbItem);
            //循环遍历把查询数据赋给新对象
            tbItemList.forEach(item ->{
                Item item1 = new Item();
                item1.setId(item.getId());
                item1.setBrand(item.getBrand());
                item1.setCategory(item.getCategory());
                item1.setImage(item.getImage());
                item1.setPrice(item.getPrice());
                item1.setSeller(item.getSeller());
                item1.setGoodsId(item.getGoodsId());
                item1.setTitle(item.getTitle());
                list.add(item1);
            });
            //3.导入到索引库中
            tbItemDao.saveAll(list);
            return new ResponseResult(200,"索引更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"索引更新失败");
        }
    }





}
